package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * 集合转换为数组
 * Collection提供了方法:toArray可以将一个集合转换为数组
 */
public class CollectionToArrayDemo {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        System.out.println("集合:"+c);//集合:[one, two, three, four, five]
        //Object[] array = c.toArray();

        String[] array = c.toArray(new String[c.size()]);
        System.out.println("数组:"+ Arrays.toString(array));//数组:[one, two, three, four, five]

        String[] array1 = c.toArray(new String[10]);
        System.out.println("数组:"+Arrays.toString(array1));//数组:[one, two, three, four, five, null, null, null, null, null]

        String[] array2 = c.toArray(new String[1]);
        System.out.println("数组:"+Arrays.toString(array2));//数组:[one, two, three, four, five]   当数组长度不足以装下集合时
    }
}
